<?php
/**
 * Created by PhpStorm.
 * User: lixiaohui
 * Date: 2015/9/21
 * Time: 17:31
 */

namespace Home\Controller;


use Think\Controller;
use Think\Log;

/**
 * Class AccountController
 * @package Home\Controller 个人中心
 */
class AccountController extends Controller
{
    public function index()
    {
        $memberTable = M("wx_member_info");
        //取微信信息
        $memberModel = $memberTable->where("openId='%s'", getOpenId())->field('id,nickname,headimgurl')->find();
        $sub_sub_button = array(
            array("url"=>"/?m=Home&c=Account&a=info","sub_sub_button"=>"个人信息"),
            array("url"=>"/?m=Home&c=Bind&a=index","sub_sub_button"=>"用户绑定"),
        );

        $this->assign("menu", $sub_sub_button);
        $this->assign("memberModel", $memberModel);
        $this->assign("bottomMenu", getBottomMenu());
        $this->display();
    }

    /**
     * 个人信息
     */
    public function info()
    {
        $infoModel = M("wx_member_info")->where("openId='%s' and name !='' and name is not null and phone !='' and phone is not null", getOpenId())
            ->find();
        //dump($infoModel);
        //若还未填写信息，即第一次登入，则跳转至登入界面
        if ($infoModel == null) {
            $this->redirect('fill');
        }
        $infoModel = M('wx_member_info')->where("openId='%s'", getOpenId())->field('name,phone,email')->find();
        $this->assign('infoModel', $infoModel);
        $this->display();
    }

    public function modify()
    {
        $data['name'] = I("name");
        $data['phone'] = I("phone");
        $email = I('email');
        //验证是否是员工
        $args = array(
            'name'=>$data['name'],
            'tel'=>$data['phone']
        );
        $postString = getPostString('GetRy',$args);
        $retStr = post(C('INTERFACE_BASE_URL'),$postString);
        if($retStr == ''){//请求接口失败
            $this->ajaxReturn(array("status" => "fail", "msg" => "服务器出问题啦!"));
        }
        Log::write(C('INTERFACE_BASE_URL').'?'.$postString);
        Log::write($retStr);
        $retObj = json_decode($retStr,true);
        if(intval($retObj['retcode']) != 0){//非员工
            $this->ajaxReturn(array("status" => "fail", "msg" => $retObj['errmsg']));
        }
        if($email != ''){
            $data['email'] = I("email");
        } else{
            $data['email'] = '';
        }
        $saveResult = M('wx_member_info')->where("openId='%s'", getOpenId())->save($data);
        if ($saveResult > 0) {
            $this->ajaxReturn(array("status" => 'success', "msg" => "修改成功!"));
        } else {
            $this->ajaxReturn(array("status" => 'fail', "msg" => "修改失败!"));
        }
    }

    //首次登入填写信息
    public function fill()
    {
        $this->display();
    }

    //记录用户填写的信息
    public function doFill()
    {
        $data['name'] = I('name');
        $data['phone'] = I('phone');
        $data['email'] = I('email');

        Log::write(json_encode($data));
        //验证是否是员工
        $args = array(
            'name'=>$data['name'],
            'tel'=>$data['phone']
        );
        $postString = getPostString('GetRy',$args);
        $retStr = post(C('INTERFACE_BASE_URL'),$postString);
        if($retStr == ''){
            $this->ajaxReturn(array("status" => "fail", "msg" => "服务器出问题啦!"));
        }
        Log::write(C('INTERFACE_BASE_URL').'?'.$postString);
        Log::write($retStr);
        $retObj = json_decode($retStr,true);
        if(intval($retObj['retcode']) != 0){//非员工
            $this->ajaxReturn(array("status" => "fail", "msg" => $retObj['errmsg']));
        }
        $saveResult = M("wx_member_info")->where("openId='%s'", getOpenId())->save($data);
        if ($saveResult > 0) {
            $this->ajaxReturn(array("status" => "success", "msg" => "保存成功!"));
        } else {
            $this->ajaxReturn(array("status" => "fail", "msg" => "保存失败!"));
        }
    }
}